package com.app.jzgdirector.common.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.jzgdirector.R;


/**
 * Created by zhangjianhui401 on 16/10/20.
 * 作为ProgressBar的View，可在业务Activity的布局中直接使用
 * 作用：可避免Dialog样式的ProgressBar覆盖全屏，影响顶部栏、菜单栏操作
 */
public class ProgressBarLayout extends RelativeLayout {

    private Context mContext = null;

    private RelativeLayout mLoadingLayout;
    private TextView mContentTView;

    private RelativeLayout mLoadingWithBgLayout;
    private TextView mContentBgTView;

    private boolean mNeedShowBg = false;

    public ProgressBarLayout(Context context) {
        super(context);
        init(context);
    }

    public ProgressBarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ProgressBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.dialog_wait_progress_layout, this);

        mLoadingLayout = (RelativeLayout) findViewById(R.id.dialog_layout);
        mContentTView = (TextView) findViewById(R.id.dialog_content_tview);

        mLoadingWithBgLayout = (RelativeLayout) findViewById(R.id.dialog_bg_layout);
        mContentBgTView = (TextView) findViewById(R.id.dialog_bg_content_tview);
    }

    /**
     * 显示对话框
     *
     * @param showWithBg 是否显示半透明黑色背景
     */
    public void show(boolean showWithBg) {
        mNeedShowBg = showWithBg;
        if (showWithBg) {
            mLoadingLayout.setVisibility(View.GONE);
            mLoadingWithBgLayout.setVisibility(View.VISIBLE);
        } else {
            mLoadingWithBgLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.VISIBLE);
        }
        setVisibility(View.VISIBLE);
    }

    /**
     * 显示对话框
     *
     * @param showWithBg  是否显示半透明黑色背景
     * @param loadingHint 提示语
     */
    public void show(boolean showWithBg, String loadingHint) {
        mNeedShowBg = showWithBg;
        if (showWithBg) {
            setLoadingHint(showWithBg, loadingHint, mContentBgTView);
            mLoadingLayout.setVisibility(View.GONE);
            mLoadingWithBgLayout.setVisibility(View.VISIBLE);
        } else {
            setLoadingHint(showWithBg, loadingHint, mContentTView);
            mLoadingWithBgLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.VISIBLE);
        }
        setVisibility(View.VISIBLE);
    }

    public void updateLoadingHint(String loadingHint) {
        if (!TextUtils.isEmpty(loadingHint)) {
            mContentBgTView.setText(loadingHint);
        }
    }

    /**
     * 设置内容
     */
    private void setLoadingHint(boolean showWithBg, String loadingHint, TextView textView) {
        if (!TextUtils.isEmpty(loadingHint)) {
            textView.setText(loadingHint);
            textView.setVisibility(View.VISIBLE);
        } else {
            if (showWithBg) {
                textView.setText(R.string.wait_loading);
                textView.setVisibility(View.VISIBLE);
            } else {
                textView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 显示文本字体颜色
     *
     * @param color
     */
    public void setContentTextColor(int color) {
        if (mNeedShowBg) {
            mContentBgTView.setTextColor(color);
        } else {
            mContentTView.setTextColor(color);
        }
    }

    /**
     * 隐藏界面
     */
    public void hide() {
        setVisibility(View.GONE);
    }
}
